<?php

require_once("./Model/DALBase.php");
require_once("./Model/User.php");
require_once("./Model/Image.php");

class UserController {
	
	/**
	 * @var NavigationView
	 */
	private $navView;
	/**
	 * @var UserModel
	 */
	private $userModel;
	/**
	 * @var UserPageView
	 */
	private $userPageView;
	/**
	 * @var DALBase
	 */
	private $DALBase;
	/**
	 * @var User
	 */
	private $user;
	/**
	 * @var array UserPost
	 */
	private $postList;
	
	/**
	 * @var NavigationView 	$navView
	 * @var UserPageView	$userPageView
	 * @var User			$user
	 * @var DALBase			$DALBase
	 */
	public function __construct(NavigationView 	$navView,
								UserPageView	$userPageView,
								User			$user,
								DALBase			$DALBase) {
		$this->navView = $navView;
		$this->userPageView = $userPageView;
		$this->user = $user;
		$this->DALBase = $DALBase;
		
		$this->postList = array();
	}
	
	/**
	 * @var User $user
	 * @return string HTML
	 */
	public function loadUserPage() {
		try {	
			if ($this->navView->getCurrentUsername() != "") {
				$user = $this->DALBase->getUser($this->navView->getCurrentUsername());
			}
			else {
				$user = $this->user;
			}
			
			return $this->userPageView->getHTML($user->getUsername(), 
												$user->getPicture(), 
												$this->loadPosts($user->getId()),
												$this->user);
		}
		catch (Exception $e) {
		}
	}
	
	/**
	 * @param integer $userId
	 * @return string HTML
	 */
	public function loadPosts ($userId) {
		try {
			$this->postList = $this->DALBase->getPostsOnId($userId);
			foreach ($this->postList as $post) {
				$this->loadComment($post);
			}
			return $this->userPageView->getPostsHTML($this->postList, $this->user->getId());
		}
		catch (Exception $e) {
			// Do nothing if post was not found.
		}
	}
	
	/**
	 * @param Post $post
	 */
	public function loadComment ($post) {
		try {
			$commentList = $this->DALBase->getCommentsOnId($post->getPostId());
			foreach ($commentList as $comment) {
				$post->addCommentToList($comment);
			}
		}
		catch (Exception $e) {
			// Do nothing if comment was not found.
		}
	}
								
	public function handleInput() {
		try {
			if ($this->userPageView->userWantsToAddPost()) {
				$imageString = "";
				if ($this->userPageView->userWantsToAddImage()) {
					$image = new Image($this->userPageView->getImageExtension(),
								   $this->userPageView->getImageTempName());
					$image->saveImageOnServer();
					$imageString = $image->getNewName();
				}
				$this->DALBase->addPost($this->user->getId(),
										$imageString,
										$this->userPageView->getMessage());
			}
			else if ($this->userPageView->userWantsToDeletePost()) {
				if ($this->userPageView->getSelectedUserId() == $this->user->getId()) {
					$oldImage = $this->DALBase->deletePost($this->userPageView->getSelectedPostId());
					if ($oldImage != "") {
						Image::deleteOldImage($oldImage);	
					}
				}
			} 
			else if ($this->userPageView->userWantsToEditPost()) {
				if ($this->userPageView->getSelectedUserId() == $this->user->getId()) {
					$imageString = "";
					if ($this->userPageView->userWantsToAddImage()) {
						$image = new Image($this->userPageView->getImageExtension(),
									   	   $this->userPageView->getImageTempName());
						$image->saveImageOnServer();
						$imageString = $image->getNewName();
					}
					
					$oldImage = $this->DALBase->editPost($this->userPageView->getSelectedPostId(),
													     $imageString,
										 			     $this->userPageView->getMessage());
				    if ($oldImage != null) {
				    	Image::deleteOldImage($oldImage);
				    }
				}
			}
			else if ($this->userPageView->userWantsToAddComment()) {  
				$this->DALBase->addComment($this->user->getId(),
										   $this->userPageView->getSelectedPostId(),
										   $this->userPageView->getMessage());
			}
			else if ($this->userPageView->userWantsToDeleteComment()) {
				if ($this->userPageView->getSelectedUserId() == $this->user->getId()) {
					$this->DALBase->deleteComment($this->userPageView->getSelectedCommentId());
				}
			} 
			else if ($this->userPageView->userWantsToEditComment()) {
				if ($this->userPageView->getSelectedUserId() == $this->user->getId()) {
					$this->DALBase->editComment($this->userPageView->getSelectedCommentId(),
												$this->userPageView->getMessage());
				}
			}
		}
		catch (\Exception $e) {
			
		}
	}
}
